package com.example.jiaoyidui.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.example.jiaoyidui.component.SystemProperties;
import com.example.jiaoyidui.entity.ApiRequestParam;
import com.example.jiaoyidui.entity.excel.TransPairExcel;

public class ExcelListener extends AnalysisEventListener<TransPairExcel> {

    /**
     * 逐行读取excel内容，将交易对读入list对象中
     *
     * @param row             excel的一行
     * @param analysisContext
     */
    @Override
    public void invoke(TransPairExcel row, AnalysisContext analysisContext) {
        // Starting from 1
        int sheetNo = analysisContext.getCurrentSheet().getSheetNo();
        String transPair = row.getTransPair();
        String[] split = transPair.split("_");
        ApiRequestParam parameter = ApiRequestParam.builder()
                .inTokenSymbol(split[0])
                .outTokenSymbol(split[1])
                .inTokenAddress(row.getInTokenAddress())
                .outTokenAddress(row.getOutTokenAddress())
                .transPair(transPair)
                .chainId(getChainIdBySheetNo(sheetNo))
                .baseUnitPrice(row.getBaseUnitPrice())
                .gasPrice("1")
                .build();

        if (sheetNo == 1) {
            parameter.setGasUrl("https://etherscan.io/autoUpdateGasTracker.ashx?sid=1a75411b0d66b0727b87d620b9132821");
            parameter.setGasKey("avgPrice");
            SystemProperties.bscList.add(parameter);
        } else if (sheetNo == 2) {
            parameter.setGasUrl("https://etherscan.io/autoUpdateGasTracker.ashx?sid=1a75411b0d66b0727b87d620b9132821");
            parameter.setGasKey("avgPrice");
            SystemProperties.polygonList.add(parameter);
        } else if (sheetNo == 3) {
            parameter.setGasUrl("https://etherscan.io/autoUpdateGasTracker.ashx?sid=1a75411b0d66b0727b87d620b9132821");
            parameter.setGasKey("avgPrice");
            SystemProperties.avalancheList.add(parameter);
        } else if (sheetNo == 4) {
            parameter.setGasUrl("https://etherscan.io/autoUpdateGasTracker.ashx?sid=1a75411b0d66b0727b87d620b9132821");
            parameter.setGasKey("avgPrice");
            SystemProperties.fantomList.add(parameter);
        } else if (sheetNo == 5) {
            parameter.setGasUrl("https://etherscan.io/autoUpdateGasTracker.ashx?sid=1a75411b0d66b0727b87d620b9132821");
            parameter.setGasKey("avgPrice");
            SystemProperties.ethList.add(parameter);
        }
        SystemProperties.allTransPairList.add(parameter);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    private int getChainIdBySheetNo(int sheetNo) {
        switch (sheetNo) {
            case 1:
                return 56;
            case 2:
                return 137;
            case 3:
                return 43114;
            case 4:
                return 250;
            case 5:
                return 1;
            default:
               return 56;
        }
    }
}
